package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.ShippingAddressAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerShippingAddressComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.DeleteShippingAddressModule;
import com.zw.yzk.kabu.internal.module.GetShippingAddressModule;
import com.zw.yzk.kabu.internal.module.SetDefaultShippingAddressModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.ShippingAddressModel;
import com.zw.yzk.kabu.presenter.ShippingAddressPresenter;
import com.zw.yzk.kabu.view.view.ShippingAddressView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2017/1/10.
 */

public class ShippingAddressActivity extends BaseActivity implements ShippingAddressView, ShippingAddressAdapter.OnItemTouchListener {

    @Bind(R.id.address_list)
    RecyclerView addressList;
    @Bind(R.id.empty)
    View empty;

    @Inject
    ShippingAddressPresenter presenter;
    private ShippingAddressAdapter adapter;
    private boolean addressClickble;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shipping_address;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.shipping_address))
                .setRightText(getResources().getString(R.string.add_shipping_address))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerShippingAddressComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .getShippingAddressModule(new GetShippingAddressModule())
                .setDefaultShippingAddressModule(new SetDefaultShippingAddressModule())
                .deleteShippingAddressModule(new DeleteShippingAddressModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addressClickble = getIntent().getBooleanExtra(ActivityConstant.SELECT_ADDRESS_ENABLE, false);
        initRecyclerView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getShippingAddress();
        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getAddressSuccess(List<ShippingAddressModel> list) {
        if (adapter == null) {
            adapter = new ShippingAddressAdapter(list, addressClickble);
            adapter.setOnItemTouchListener(this);
            addressList.setAdapter(adapter);
        } else {
            adapter.refresh(list);
        }

        refreshEmptyStatus(adapter.getItemCount() == 0);
    }

    @Override
    public void onSelected(int position, ShippingAddressModel model) {
        setDefaultAddress(model.getId());
    }

    @Override
    public void onDelete(int position, ShippingAddressModel model) {
        deleteShippingAddress(model.getId());
    }

    @Override
    public void onAddressSelected(int position, ShippingAddressModel model) {
        Intent data = new Intent();
        data.putExtra(ActivityConstant.SELECTED_RECEIVER_NAME, model.getName());
        data.putExtra(ActivityConstant.SELECTED_RECEIVER_PHONE, model.getPhone());
        data.putExtra(ActivityConstant.SELECTED_RECEIVER_ADDRESS, model.getAddress());
        setResult(RESULT_OK, data);
        finish();
    }

    @OnClick(R.id.right_text)
    public void addShippingAddress() {
        startActivity(new Intent(this, AddShippingAddressActivity.class));
    }

    /**
     * 是否显示空提示
     *
     * @param show true:显示，false:不显示
     */
    private void refreshEmptyStatus(boolean show) {
        empty.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        addressList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    }

    /**
     * 获取收货地址列表
     */
    private void getShippingAddress() {
        presenter.getShippingAddress();
    }

    /**
     * 设置默认收货地址
     *
     * @param id 收货地址id
     */
    private void setDefaultAddress(int id) {
        presenter.setDefaultAddress(id);
    }

    /**
     * 删除收货地址
     *
     * @param id 收货地址id
     */
    private void deleteShippingAddress(int id) {
        presenter.deleteAddress(id);
    }

}
